﻿// <copyright file="NewswirePage.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-04-12</date>
// <summary>Newswire Test Page</summary>

namespace DemoNYTimes
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using NYTimes;

    /// <summary>
    /// Newswire API test page 
    /// </summary>
    public partial class NewswirePage : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the NewswirePage class.
        /// </summary>
        public NewswirePage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Set the time period when a radio button is checked
        /// </summary>
        /// <param name="sender">the radio button</param>
        /// <param name="e">the routed event arguments</param>
        private void OnTimePeriodChecked(object sender, RoutedEventArgs e)
        {
            var newswire = this.Resources["Newswire"] as Newswire;

            var radioButton = sender as RadioButton;

            switch (radioButton.Tag as string)
            {
                case "Recent":
                    newswire.TimePeriod = 48;
                    break;

                case "Last24Hours":
                    newswire.TimePeriod = 24;
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// image failed event handler
        /// </summary>
        /// <param name="sender">the image that failed</param>
        /// <param name="e">the exception routed event arguments</param>
        private void OnImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            var image = sender as Image;

            var source = image.Source as BitmapImage;

            System.Diagnostics.Debug.WriteLine("Image error with {0}: {1}", source.UriSource, e.ErrorException.Message);
        }
    }
}
